import numpy as np
import matplotlib.pyplot as plt
def plot_entropy():
    """
    H(p) = -p log p - (1-p)log(1-p)
    :return:
    """
    p = range(1, 1000)
    # print(p)
    p = [i/1000 for i in p]
    # print(p)
    H_p_e = [(-i * np.log(i) - (1-i) * np.log(1-i)) for i in p]
    H_p_2 = [(-i * np.log2(i) - (1-i) * np.log2(1-i)) for i in p]
    H_p_10 = [(-i * np.log10(i) - (1 - i) * np.log10(1 - i)) for i in p]
    # 画图
    fig, ax = plt.subplots()
    ax.plot(p, H_p_e, c='red', label='H(P) log_e')
    ax.plot(p, H_p_2, c='blue', label='H(P) log_2')
    ax.plot(p, H_p_10, c='green', label='H(P) log_10')
    ax.legend()
    plt.show()


plot_entropy()
